// 在src目录下创建router.js
import { Message } from 'element-ui';
import Vue from 'vue';
import VueRouter from 'vue-router';
import store from '../store/index'
import Login from '../pages/login/index'
import Home from '../pages/home/index'
import PoorApply from '../pages/poor_apply/index'

Vue.use(VueRouter);

export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      hidden: true,
      title: '登录',
      icon: 'el-icon-s-home'
    }
  },
  {
    // 登陆成功后的路由
    path: '/',
    name: 'Home',
    component: () => import('../pages/home/index'),
    redirect: '/sys_home',
    meta: {
      hidden: false,
      title: '',
      icon: ''
    },
    children: [
      {
        path: '/sys_home',
        component: () => import('../pages/sys_home/index'),
        meta: {
          hidden: false,
          title: '首页',
          icon: 'el-icon-s-home'
        }
      }
    ]
  },
  {
    path: '/center',
    component: () => import('../pages/home/index'),
    name: 'Center',
    meta: {
      hidden: false,
      icon: 'el-icon-user-solid',
      title: '个人中心'
    },
    children: [
      {
        path: '/my_info',
        name: 'MyInfo',
        component: () => import('../pages/my_info/index'),
        meta: {
          hidden: false,
          title: '个人信息',
          icon: ''
        }
      },
      {
        path: '/update_password',
        name: 'UpdatePassword',
        component: () => import('../pages/update_password/index'),
        meta: {
          hidden: false,
          title: '修改密码',
          icon: ''
        }
      }
    ]
  },
  {
    path: '/notfoundpage404',
    name: 'NotFoundPage404',
    component: () => import('../pages/not_found_page/index'),
    meta: {
      hidden: true,
      title: '404',
      icon: 'el-icon-s-home'
    }
  },
  // {
  //   // 任意路由
  //   path: '/:pathMatch(.*)*',
  //   redirect: '/notfoundpage404',
  //   name: 'Any',
  //   meta: {
  //     title: '任意路由',
  //     hidden: true,
  //     icon: 'el-icon-s-home'
  //   }
  // }
]


export const asyncRoutes = [
  {
    path: '/info',
    name: 'Info',
    component: () => import('../pages/home/index'),
    redirect: '/info/notice',
    meta: {
      title: '信息管理',
      icon: 'el-icon-info',
      hidden: false
    },
    children: [
      {
        path: '/notice',
        name: 'Notice',
        component: () => import('../pages/notice/index'),
        meta: {
          title: '公告信息',
          icon: '',
          hidden: false
        }
      },
      {
        path: '/information',
        name: 'Information',
        component: () => import('../pages/information/index'),
        meta: {
          title: '扶贫资讯',
          icon: '',
          hidden: false
        }
      },
      {
        path: '/policy',
        name: 'Policy',
        component: () => import('../pages/policy/index'),
        meta: {
          title: '扶贫政策',
          icon: '',
          hidden: false
        }
      },
      {
        path: '/agritainment',
        name: 'Agritainment',
        component: () => import('../pages/agritainment/index'),
        meta: {
          title: '农家乐信息',
          icon: '',
          hidden: false
        }
      },
      {
        path: '/commodity',
        name: 'Commodity',
        component: () => import('../pages/commodity/index'),
        meta: {
          title: '商品管理',
          icon: '',
          hidden: false
        }
      },
      {
        path: '/orders',
        name: 'Orders',
        component: () => import('../pages/orders/index'),
        meta: {
          title: '订单管理',
          icon: '',
          hidden: false
        }
      },
      {
        path: '/comments',
        name: 'Comments',
        component: () => import('../pages/comments/index'),
        meta: {
          title: '政策评论',
          icon: '',
          hidden: false
        }
      }
    ]
  },
  {
    path: '/apply',
    name: 'Apply',
    component: Home,
    redirect: '/apply/poor_apply',
    meta: {
      title: '申请管理',
      hidden: false,
      icon: 'el-icon-s-order'
    },
    children: [
      {
        path: '/poor_apply',
        name: 'PoorApply',
        component: PoorApply,
        meta: {
          hidden: false,
          title: '贫困户申请',
          icon: ''
        }
      },
      {
        path: '/project_apply',
        name: 'ProjectApply',
        component: () => import('../pages/project_apply/index'),
        meta: {
          hidden: false,
          title: '扶贫项目申请',
          icon: ''
        }
      }
    ]
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../pages/home/index'),
    redirect: '/user/admin_management',
    meta: {
      title: '用户管理',
      icon: 'el-icon-s-custom',
      hidden: false
    },
    children: [
      {
        path: '/admin_management',
        name: 'AdminManagement',
        component: () => import('../pages/admin_management/index'),
        meta: {
          hidden: false,
          title: '管理员',
          icon: ''
        }
      },
      {
        path: '/user_management',
        name: 'UserAanagement',
        component: () => import('../pages/user_management/index'),
        meta: {
          hidden: false,
          title: '普通用户',
          icon: ''
        }
      }
    ]
  },
  {
    path: '/permiss',
    name: 'Permiss',
    redirect: '/permiss/permission',
    component: () => import('../pages/home/index'),
    meta: {
      title: '权限管理',
      hidden: false,
      icon: 'el-icon-s-grid'
    },
    children: [
      {
        path: '/permission',
        name: 'Permission',
        component: () => import('../pages/permission/index'),
        meta: {
          hidden: false,
          title: '角色管理',
          icon: ''
        }
      },
      //admin_permission
      {
        path: '/admin_permission',
        name: 'AdminPermission',
        component: () => import('../pages/admin_permission/index'),
        meta: {
          hidden: false,
          title: '管理员权限',
          icon: ''
        }
      },
    ]
  }
]



const router = new VueRouter({
  mode: 'history',
  routes: constantRoutes

})

function filtersRoleRoutes(asyncRoutes, routes) {
  return asyncRoutes.filter(item => {
    if (routes.includes(item.path)) {
      if (item.children && item.children.length > 0) {
        item.children = filtersRoleRoutes(item.children, routes)
      }
      return true
    }
  })
}

const setRoutes = () => {
  let menuList = JSON.parse(localStorage.getItem('menus'))
  if (menuList) {
    let asyncRoutes1 = asyncRoutes
    let userRouters = filtersRoleRoutes(asyncRoutes1, menuList)
    if (userRouters) {
      userRouters.forEach(item => {
        router.addRoute(item)
      })
    }
  }

  if (!router.currentRoute.path.startsWith('/')) {
    router.addRoute({
      // 任意路由
      path: '/:pathMatch(.*)*',
      redirect: '/notfoundpage404',
      name: 'Any',
      meta: {
        title: '任意路由',
        hidden: true,
        icon: 'el-icon-s-home'
      }
    })
  }

}

setRoutes()

// 前置路由守卫
router.beforeEach((to, from, next) => {

  // to and from are both route objects. must call `next`.
  let token = store.state.userStore.token
  if (token) {
    if (to.path !== '/login') {
      next()
    } else {
      next('/')
    }
  } else {
    // 用户未登录
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }

})

export default router
